<?php
namespace App\Services;

use Exception;
use EasyWeChat\Factory;
use App\Models\DedustingUser;
use Overtrue\EasySms\EasySms;

class DedustingMiniService
{
    public function __construct()
    {
        $config = $this->getConfig();
        $this->app = Factory::miniProgram($config);
    }

    protected function getConfig()
    {
        $config = [
            'app_id' => env('DEDUSTING_APP_ID'),
            'secret' => env('DEDUSTING_APP_SECRET')
        ];
        return $config;
    }

    public function parseLoginCode($code)
    {
        $res = $this->app->auth->session($code);
        
        $user = DedustingUser::firstOrCreate(['openid' => $res['openid']]);

        $res['user_id'] = $user->id;
        $res['mobile'] = $user->mobile;

        return $res;

    }

    public function makeDevicePush($data)
    {
        // $this->subscribe_message->send($data);
        
    }
}